<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>javascript</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      body {
        font-size: 14px;
        padding: 20px;
      }
      .div {
        line-height: 30px;
        width: 100px;
        background: red;
        height: 10px;
        background-image: url();
      }
    </style>
  </head>
  <body>
    <ul>
      <li onclick="getD()">获取数据</li>
    </ul>
  </body>
  <script>
    function getD() {
      new Promise((resolve, reject) => {
        reject("error");
      })
        .then((data) => {
          console.log(data);
        })
        .catch((data) => {
          console.log(data);
        });
    }
    function getData(t) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(t);
        }, t);
      });
    }
    var p1 = new Promise((resolve, reject) => {
      resolve("p1");
    });
    var p2 = new Promise((resolve, reject) => {
      reject();
    }).catch((err) => {
      return Promise.resolve("aaab");
    });

    Promise.all([p2, p1]).then((data) => {
      console.log(data);
    });
    Promise.race([p2, p1]).then((data) => {
      console.log(data);
    });
    // 封装的数据请求方法
    function $ajax(t) {
      return new Promise((resolve, reject) => {
        // 此处用setTimeout替代ajax异步请求
        setTimeout(() => {
          if (t > 2000) {
            resolve(t);
          } else {
            reject("传递的时间太短");
          }
        }, t);
      });
    }
    const t1 = $ajax(4000);
    const t2 = $ajax(3500);
    const t3 = $ajax(3000);
    Promise.all([t1, t2, t3]).then(
      (data) => {
        console.log(data); // [4000, 3500, 3000]
      },
      (error) => {
        console.log(error);
      }
    );
  </script>
</html>
